登录 白背景

leetcode/1-100/54. 螺旋矩阵.md

丑陋的写法,我觉得明天我就看不懂了

func spiralOrder(matrix [][]int) []int {
    x := 0
    y := 0
    ret := []int{matrix[0][0]}
    edgeTop := 0
    edgeLeft := -1
    edgeRight := len(matrix[0])
    edgeBottom := len(matrix)
    now := 6//2 4 8 6
    for {
        if now == 6 {
            if x + 1 < edgeRight {
                x++
                ret = append(ret, matrix[y][x])
                continue
            }
            edgeRight--
            if y + 1 < edgeBottom {
                y++
                ret = append(ret, matrix[y][x])
                now = 2
                continue
            }
            break
        }
        if now == 2 {
            if y + 1 < edgeBottom {
                y++
                ret = append(ret, matrix[y][x])
                continue
            }
            edgeBottom--
            if x - 1 > edgeLeft {
                x--
                ret = append(ret, matrix[y][x])
                now = 4
                continue
            }
            break
        }
        if now == 4 {
            if x - 1 > edgeLeft {
                x--
                ret = append(ret, matrix[y][x])
                continue
            }
            edgeLeft++
            if y - 1 > edgeTop {
                y--
                ret = append(ret, matrix[y][x])
                now = 8
                continue
            }
            break
        }
        if now == 8 {
            if y - 1 > edgeTop {
                y--
                ret = append(ret, matrix[y][x])
                continue
            }
            edgeTop++
            if x + 1 < edgeRight {
                x++
                ret = append(ret, matrix[y][x])
                now = 6
                continue
            }
            break
        }
    }
    return ret
}